<?php


namespace app\api\controller;


use think\Request;



class ApiController
{
    protected $request;

    protected $db;

    protected $page = 1; //分页查找页

    protected $pageSize = 10;

    protected $tableName;

    protected $param; //

    public function __construct()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            header("Access-Control-Allow-Origin: *");
            exit;
        }
        $this->request = Request::instance();
        $this->param = input();
        $this->page = empty($this->param['page']) ? 1 : $this->param['page'];
        $this->pageSize = empty($this->param['pageSize']) ? $this->pageSize : $this->param['pageSize']; //一页多少条记录
    }
    /**
     * api 分页模糊查询
     */
    public function datalist()
    {
        $page = $this->page >= 1 ? ($this->page - 1) : 0;
        $page = $page * $this->pageSize;

        $data = $this->db->limit($page, $this->pageSize)->select();
        $isEnd = count($data) == $this->pageSize ? false : true;
        return ['isend' => $isEnd, 'data' => $data];
    }


    public function add()
    {
        unset($this->param['id']);
        return $this->db->insertGetId($this->param);
    }

    public function edit()
    {
        if (empty($this->param['id'])) apiError('参数丢失');
        $this->db->where('id', '=', $this->param['id']);
        return $this->db->update($this->param);
    }
}
